package com.million.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.million.eduservice.entity.EduSubject;
import com.million.eduservice.entity.excel.SubjectData;
import com.million.eduservice.service.EduSubjectService;
import com.million.servicebase.exception.GuliException;

/**
 * @Author: studyboy
 * @Date: 2022/3/23  18:00
 */
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    //注意我们这里不能将SubjectExcelListener交给spring管理,需要自己手动去new
    //不能实现数据库操作   可以手动操作
    //将EduSubjectService 手动注入到SubjectExcelListener中
    public EduSubjectService subjectService;

    public SubjectExcelListener () {
    }

    public SubjectExcelListener (EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }

    //读取excel内容，一行一行进行读取
    @Override
    public void invoke (SubjectData subjectData, AnalysisContext analysisContext) {
        //先判断数据是否为空
        if (subjectData == null) {
            throw new GuliException(20001, "文件数据为空");
        }

        //一行一行读取，每次读取有两个值，第一个值一级分类，第二个值二级分类
        //判断一级分类是否冲重复
        EduSubject existOneSubject = this.existOneSubject(subjectService, subjectData.getOneSubjectName());
        if (existOneSubject == null) {
            //添加一级分类到数据库中
            existOneSubject = new EduSubject();
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(subjectData.getOneSubjectName());
            subjectService.save(existOneSubject);
        }
        //如果有一级分类或者没有但是添加了，就可以获取一级分类的id
        String pid = existOneSubject.getId();

        //判断二级分类是否冲重复
        EduSubject existTwoSubject = this.existTwoSubject(subjectService, subjectData.getTwoSubjectName(), pid);
        if (existTwoSubject == null) {
            //添加二级分类到数据库中
            existTwoSubject = new EduSubject();
            existTwoSubject.setParentId(pid);
            existTwoSubject.setTitle(subjectData.getTwoSubjectName());
            subjectService.save(existTwoSubject);
        }
    }

    /**
     * 判断一级分类是否重复
     *
     * @param subjectService 可以用来进行数据库操作
     * @param title          一级分类的标签，比如是前端还是后端开发
     * @return 返回EduSubject
     */
    private EduSubject existOneSubject (EduSubjectService subjectService, String title) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", title);
        wrapper.eq("parent_id", "0");
        EduSubject oneSubject = subjectService.getOne(wrapper);
        return oneSubject;
    }

    /**
     * 判断二级分类是否重复
     *
     * @param subjectService 可以用来进行数据库操作
     * @param title          二级分类的标签，比如是vue 还是js、java
     * @param parent_id      父id，一级分类的id
     * @return 返回EduSubject
     */
    private EduSubject existTwoSubject (EduSubjectService subjectService, String title, String parent_id) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title", title);
        wrapper.eq("parent_id", parent_id);
        EduSubject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }

    @Override
    public void doAfterAllAnalysed (AnalysisContext analysisContext) {

    }
}
